package cn.edu.neusoft.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.yuejian.R;
import cn.edu.neusoft.yuejian.bean.user;
import cn.edu.neusoft.yuejian.model.usermodel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private EditText etUid,etPwd;
    private Button b_login;
    private TextView b_wj,b_zc;
    private CheckBox checkBox;
    private user user;
    private String userid;
    static String YES = "yes";
    static String NO = "no";
    private String isMemory = "";//isMemory变量用来判断SharedPreferences有没有数据，包括上面的YES和NO
    private String FILE = "saveUserNamePwd";
    static String name, pass;
    private SharedPreferences spp = null;//声明一个SharedPreferences
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        initData();
    }



    private void initViews() {
        etUid = (EditText) findViewById(R.id.log_edit_user);
        etPwd = (EditText) findViewById(R.id.log_edit_pas);
        b_login = (Button) findViewById(R.id.log_but_log);
      b_wj=(TextView)findViewById(R.id.login_wj);
        b_zc=(TextView)findViewById(R.id.login_zc) ;
        checkBox=(CheckBox)findViewById(R.id.log_checkBox);
    }
    private void initEvents() {
        b_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                remenber();
                usermodel usermodel=new usermodel();
                Call<user> call=usermodel.login(etUid.getText().toString(),etPwd.getText().toString());
                call.enqueue(new Callback<user>() {
                    @Override
                    public void onResponse(Call<user> call, Response<user> response) {
                        if (response.isSuccessful()){
                            user = response.body();
                            userid = user.getId();
                            if (!userid.equals("0")){
                                saveUser(userid);
                                Intent intent = new Intent(LoginActivity.this,
                                        MainActivity.class);

                                LoginActivity.this.startActivity(intent);
                                finish();
                            }else{
                                Toast.makeText(LoginActivity.this,
                                        "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }
                        }

                    }

                    @Override
                    public void onFailure(Call<user> call, Throwable t) {
                        Toast.makeText(LoginActivity.this,
                                "与服务器通讯失败，请检查网络设置！",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        b_zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,
                       RegActivity.class);
                LoginActivity.this.startActivity(intent);
            }
        });
    }
    private void initData() {
        spp = getSharedPreferences(FILE, MODE_PRIVATE);
        isMemory = spp.getString("isMemory", NO);
        if (isMemory.equals(YES)) {
            name = spp.getString("name", "");
            pass = spp.getString("password", "");
            etUid.setText(name);
            etPwd.setText(pass);
            SharedPreferences.Editor editor = spp.edit();
            editor.putString(name, etUid.toString());
            editor.putString(pass, etPwd.toString());
            editor.commit();
        }
    }
    private void saveUser(String userid) {
        SharedPreferences sharp=getSharedPreferences("login",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharp.edit();
        editor.putString("username",etUid.getText().toString());
        editor.putString("userid",userid);
        editor.putString("userpas",etPwd.getText().toString());
        editor.commit();
    }
    public void remenber(){
        if (checkBox.isChecked()) {
            if (spp == null) {
                spp = getSharedPreferences(FILE, MODE_PRIVATE);
            }
            SharedPreferences.Editor edit = spp.edit();
            edit.putString("name", etUid.getText().toString());
            edit.putString("password", etPwd.getText().toString());
            edit.putString("isMemory", YES);
            edit.commit();
        } else if (!checkBox.isChecked()) {
            if (spp == null) {
                spp = getSharedPreferences(FILE, MODE_PRIVATE);
            }
            SharedPreferences.Editor edit = spp.edit();
            edit.putString("isMemory", NO);
            edit.commit();
        }
    }
}
